<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Family extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'eb_family';

    public static $gender = [0=>'未设置',1=>'男',2=>'女'];
    public static $typeTxt=[
        1=>'儿童',
        2=>'孕妇',
        3=>'其他'
    ];
    public static $idCardTypeTxt=[
        1=>'护照',
        2=>'港澳通行证',
        3=>'军官证',
        4=>'身份证',
    ];
    public function hospital(){
        return $this->hasOne(Hospital::class,'id','hospital_id');
    }

    public function user(){
        return $this->hasOne(User::class,'id','user_id');
    }

    public function child()
    {
        return $this->hasMany(FamilyChild::class,'family_id','id');
    }
    public function childOne()
    {
        return $this->hasOne(FamilyChild::class,'family_id','id');
    }

    public function pregn()
    {
        return $this->hasOne(FamilyPregn::class,'family_id','id');
    }

    public function sign(){
        return $this->hasOne(HospitalUserFamily::class,'family_id','id')->ofMany([
            'starttime' => 'max',
        ], function ($query) {
            $query->where('status', 1);
        });
    }

    public function team()
    {
        $sign = $this->sign;
        return isset($sign)?$sign->team():"";
    }


    public function import()
    {
        return $this->hasOne(ImportFamily::class,'family_id','id');
    }
    public function getPhoneAttribute($value)
    {
        if(!$value){
            return $this->user?$this->user->phone:"";
        }
        return $value;
    }
    public function getSignDateAttribute($value)
    {
        return "2024-10-10";
    }
}
